//
//  UIImageView+JTC.m
//  JTProject
//
//  Created by Yuki on 2021/3/9.
//

#import "UIImageView+EL.h"
#import <objc/runtime.h>
#import <UIImageView+WebCache.h>
#import "UIImage+EL.h"
@implementation UIImageView (JTC)
/// 创建一个图片
+ (UIImageView *)el_createImgView{
    return [self _createImgViewWithImg:nil];
}

/// 创建一个图片
/// @param img 图片
+ (UIImageView *)el_createImgViewWithImg:(UIImage *)img{
    return [self _createImgViewWithImg:img];
}

///private 创建一个图片
+ (UIImageView *)_createImgViewWithImg:(UIImage *)img{
    UIImageView *imgView = [[UIImageView alloc]init];
    imgView.contentMode = UIViewContentModeScaleAspectFill;
    imgView.clipsToBounds = YES;
    if (img) {
        imgView.image = img;
    }
    return imgView;
}

/**  动态添加属性  **/
///get方法
- (EL_ClickCurrentImgViewBlock)clickCurrentImgViewBlock{
    return objc_getAssociatedObject(self, _cmd);
}
///set方法
- (void)setClickCurrentImgViewBlock:(EL_ClickCurrentImgViewBlock)clickCurrentImgViewBlock{
    objc_setAssociatedObject(self, @selector(clickCurrentImgViewBlock), clickCurrentImgViewBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
    
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickImgView)]];
}
///点击图片
- (void)clickImgView{
    EL_ClickCurrentImgViewBlock block = (EL_ClickCurrentImgViewBlock )objc_getAssociatedObject(self, @selector(clickCurrentImgViewBlock));
    if (block) {
        block(self);
    }
}

#pragma mark - --------- 设置网络图片  ---------
- (void)sd_setFadeImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder
{
    __weak __typeof(self)wself = self;
    [self sd_setImageWithURL:url placeholderImage:placeholder completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if (image) {
            CATransition *animation = [CATransition animation];
            [animation setDuration:0.65f];
            [animation setType:kCATransitionFade];
            animation.removedOnCompletion = YES;
            [wself.layer addAnimation:animation forKey:@"transition"];
        }
    }];
    [self.layer removeAnimationForKey:@"transition"];
}

- (void)sd_setFadeImageWithURL:(NSURL *)url
{
    __weak __typeof(self)wself = self;
    if (@available(iOS 13.0, *)) {
        [self sd_setImageWithURL:url placeholderImage:[UIImage imageWithColor:[UIColor placeholderTextColor]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            if (image) {
                CATransition *animation = [CATransition animation];
                [animation setDuration:0.65f];
                [animation setType:kCATransitionFade];
                animation.removedOnCompletion = YES;
                [wself.layer addAnimation:animation forKey:@"transition"];
            }
        }];
    } else {
        [self sd_setImageWithURL:url placeholderImage:[UIImage imageWithColor:UIColor.lightTextColor] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            if (image) {
                CATransition *animation = [CATransition animation];
                [animation setDuration:0.65f];
                [animation setType:kCATransitionFade];
                animation.removedOnCompletion = YES;
                [wself.layer addAnimation:animation forKey:@"transition"];
            }
        }];
    }
    [self.layer removeAnimationForKey:@"transition"];
}
@end
